const Koa = require('koa')
const app = new Koa()

// 记录执行时间
app.use(async (ctx, next) => {
  let stime = new Date().getTime()
  await next()
  let etime = new Date().getTime()
  ctx.response.type = 'text/html'
  ctx.response.body = '<h1>Hello World</h1>'
  console.log(`请求地址: ${ctx.path}，响应时间：${etime - stime}ms`)
})

app.use(async (ctx, next) => {
  console.log('中间件1 start')
  await next()
  console.log('中间件1 end')
})

app.use(async (ctx, next) => {
  console.log('中间件2 start')
  await next()
  console.log('中间件2 end')
})

app.use(async (ctx, next) => {
  console.log('中间件3 start')
  await next()
  console.log('中间件3 end')
})
/**
 * next 参数的作用是将处理的控制权转交给下一个中间件，而 next() 后面的代码，将会在下一个中间件及后面的中间件（如果有的话）执行结束后再执行
 */
app.listen(3000, () => {
  console.log('server is running at http://localhost:3000')
})